package com.bluestone.run;

import java.util.ArrayList;

import org.apache.poi.ss.usermodel.Row;

import com.bluestone.context.ExecuteContext;
import com.bluestone.context.IContext;
import com.bluestone.context.TestCaseContext;
import com.bluestone.context.TestSuiteContext;
import com.bluestone.scripts.Attributes;
import com.bluestone.scripts.Property;
import com.bluestone.scripts.TestCase;
import com.bluestone.scripts.TestSuite;
import com.bluestone.util.Util;

public class TestSuiteController extends BaseController {

	@Override
	boolean executeContext(IContext context) {
		boolean flag =true;
		TestSuiteContext testsuiteContext = (TestSuiteContext)context;
		
		TestSuite testsuite = testsuiteContext.getTestSuite();	
		testsuiteContext.addExecuteLog("run testsuite "+testsuite.getName());
		ArrayList testcases = (ArrayList) testsuite.getTestcases();
		boolean needstandalone = false;
		int failcount = 0;
		TestCaseController controller = new TestCaseController();
		for (int i = 0; i < testcases.size(); i++) {
			TestCase testcase = (TestCase) testcases.get(i);
			testcase.setParent(testsuite);
			if (needstandalone) {
				if (Attributes.FALSE.equalsIgnoreCase(testcase.getStandalone())) {
					continue;
				}
			}
			if (testcase.getName().equalsIgnoreCase(Attributes.RESET)) {
				continue;
			}
			TestCaseContext testcasecontext = testsuiteContext.getTestCaseContext(testcase);

			int retry = 0;
			boolean testcaseflag = false;
			while(true){
				testcaseflag = controller.execute(testcasecontext);
				if (!testcaseflag
						&& retry <= Integer.parseInt(testcasecontext.getProjectContext().getAllowRetryCount())
						&& !testcase.getName().equalsIgnoreCase("reset")) {
					testcaseflag = true;
					testcasecontext.addExecuteLog("retry run testcase " + testcase.getName() + " "
							+ retry + " times");
					runResetCase(testsuiteContext);
					retry++;
				} else {
					break;
				}				
			}

			String result = "pass";
			if (!testcaseflag) {
				result = "failed";
			}
			if (!testcaseflag) {
				failcount++;
				runResetCase(testsuiteContext);
				needstandalone = true;
			} else {
				needstandalone = false;
			}
			if (failcount > Integer.parseInt(testsuiteContext.getProjectContext().getAllowFailureCount())) {
				flag = false;
				break;
			}
		}		
		return flag;
		
	}
	
	private void runResetCase(TestSuiteContext testsuiteContext) {
		ArrayList list = (ArrayList) testsuiteContext.getTestSuite().getTestcases();
		for (int i = 0; i < list.size(); i++) {
			TestCase temp = (TestCase) list.get(i);
			if (temp.getName().equalsIgnoreCase("reset")) {
				Util.info("run reset testcase");
				TestCaseContext context = testsuiteContext.getTestCaseContext(temp);
				TestCaseController controller = new TestCaseController();
				controller.execute(context);
				boolean flag = controller.execute(context);
				if (flag) {
					Util.info("run reset testcase sucessful!");
				} else {
					Util.info("run reset testcase failure!");
					Util.warn("run reset testcase failure!");
				}

			}
		}
	}
	


}
